/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.broker.consumer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.broker.cons.MessageCons;
import me.send.broker.cons.OrderCons;
import me.send.broker.cons.ProgressCons;
import me.send.broker.entity.Message;
import me.send.broker.entity.OrderProgress;
import me.send.broker.entity.base.ReturnBean;
import me.send.broker.enums.AuthodType;
import me.send.broker.pojo.OrderInfoBean;
import me.send.broker.pojo.OrderInfoV1_1Bean;
import me.send.broker.pojo.OrderInfoV1_1_1Bean;
import me.send.broker.pojo.OrderInfoV1_1_2Bean;
import me.send.broker.pojo.TaskBean;
import me.send.business.base.jpush.JPush;
import me.send.framework.util.HelperUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broker.consumer.controller.base.BaseController;

/**
 * @author maxmin
 *
 * @date 2015年8月4日 下午5:47:22
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController extends BaseController {

	/**
	 * 经纪人抢单
	 * @param popid
	 * @param orderid
	 * @param devicenumber
	 * @param longitude
	 * @param latitude
	 * @return
	 * @author maxmin
	 * @date 2015年9月8日上午9:17:25
	 */
	@RequestMapping(value = "/compet")
	@ResponseBody
	public ReturnBean competOrder(int popid, int orderid, String devicenumber,
			String longitude, String latitude) {
		ReturnBean returnBean = new ReturnBean();
		try {
			int result = orderService.competing(popid, orderid, longitude,latitude);
			if (result == OrderCons.ONE_ROW) {
				//异步操作
				final int oid = orderid;
				new Thread(new Runnable() {
					public void run() {
						String accountId = orderService.getUserByOrderId(oid);
						// 发送给用户被抢单的通知
						JPush.pushWithAlias(JPush.getUserJPushClient(),
								OrderCons.COMPETED_STR, accountId);
						//记录消息
						int userid = orderService.getUserIdByOrderId(oid);
						Message message = new Message(OrderCons.COMPETED_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
						saveMessage(message);
						//记录进度
						OrderProgress op = new OrderProgress();
						op.setUserMsg(ProgressCons.ORDER_GRABED_MSG);
						op.setContent(ProgressCons.ORDER_GRABED_CONTENT);
						op.setOrderid(oid);
						op.setOrderStatus(OrderCons.COMPETED);
						op.setCreatetime(HelperUtil.getCurrentDate());
						op.setUpdatetime(HelperUtil.getCurrentDate());
						orderProgressService.addOrderProgress(op);
					}
				}).start();
				
				returnBean.setCode(ReturnBean.SUCCESS);
				returnBean.setMessage(ReturnBean.SUCCESS_STR);
			}else if (result == OrderCons.LIMET_COMPET_TIME) {
				returnBean.setMessage(OrderCons.LIMET_COMPET_TIME_STR);
				returnBean.setCode(OrderCons.LIMET_COMPET_TIME);
			}else if (result == OrderCons.LIMET_ORDER_BY_SELF) {
				returnBean.setMessage(OrderCons.LIMET_ORDER_BY_SELF_STR);
				returnBean.setCode(OrderCons.LIMET_ORDER_BY_SELF);
			}else if (result == OrderCons.HAS_COMPETED) {
				returnBean.setMessage(OrderCons.HAS_COMPETED_STR);
				returnBean.setCode(OrderCons.HAS_COMPETED);
			}else if (result == 0) {
				returnBean.setMessage("请先通过认证再抢单吧");
				returnBean.setCode(5);
			}else if (result==1) {
				returnBean.setMessage("申请认证中，请通过认证后再抢单");
				returnBean.setCode(6);
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"OrderController", "competOrder", e);
			e.printStackTrace();
		}
		return returnBean;
	}

	/**
	 * 经纪人端已购买
	 * 
	 * @param popid
	 *            经纪人id
	 * @param orderid
	 *            订单id
	 * @return
	 */
	@RequestMapping(value = "/purchas")
	@ResponseBody
	public ReturnBean purchased(int popid, int orderid, double fee,int adverttype) {
		ReturnBean returnBean = new ReturnBean();
		try {
			int result = orderService.purchased(popid, orderid, fee,adverttype);
			if (result > 0) {
				final String accountId = orderService.getUserByOrderId(orderid);
				final int oid = orderid;
				new Thread(new Runnable() {
					public void run() {
						// 发送给用户被抢单的通知
						JPush.pushWithAlias(JPush.getUserJPushClient(),OrderCons.PURCHASED_STR, accountId);
						//记录消息
						int userid = orderService.getUserIdByOrderId(oid);
						Message message = new Message(OrderCons.PURCHASED_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
						saveMessage(message);
						//保存进度
						OrderProgress op= new OrderProgress();
						op.setUserMsg(ProgressCons.ORDER_BUYED_MSG);
						op.setContent(ProgressCons.ORDER_BUYED_CONTENT);
						op.setOrderid(oid);
						op.setOrderStatus(OrderCons.PURCHASED);
						op.setCreatetime(HelperUtil.getCurrentDate());
						op.setUpdatetime(HelperUtil.getCurrentDate());
						orderProgressService.addOrderProgress(op);
					}
				}).start();
				returnBean.setCode(ReturnBean.SUCCESS);
				returnBean.setMessage(ReturnBean.SUCCESS_STR);
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"OrderController", "purchased", e);
			e.printStackTrace();
		}
		return returnBean;
	}

	/**
	 * 经纪人端已完成
	 * 
	 * @param popid
	 *            经纪人id
	 * @param orderid
	 *            订单id
	 * @return
	 */
	@RequestMapping(value = "/finish")
	@ResponseBody
	public ReturnBean finished(int popid, int orderid) {
		ReturnBean returnBean = new ReturnBean();
		try {
			int result = orderService.finished(popid, orderid);
			if (result > 0) {
				final String accountId = orderService.getUserByOrderId(orderid);
				final int oid = orderid;
				new Thread(new Runnable() {
					public void run() {
						OrderInfoBean info = orderService.getOrderInfo(oid);
						// 发送给用户被抢单的通知
						if (info!=null) {
							if (info.getType()==OrderCons.ORDER_TYPE_DAIBAN) {
								JPush.pushWithAlias(JPush.getUserJPushClient(),OrderCons.FINISHED_STR, accountId);
								//记录消息
								int userid = orderService.getUserIdByOrderId(oid);
								Message message = new Message(OrderCons.FINISHED_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
								saveMessage(message);
								//保存进度
								OrderProgress op= new OrderProgress();
								op.setUserMsg(ProgressCons.ORDER_FINISHED_MSG);
								op.setContent(ProgressCons.ORDER_FINISHED_CONTENT);
								op.setOrderid(oid);
								op.setOrderStatus(OrderCons.FINISHED);
								op.setCreatetime(HelperUtil.getCurrentDate());
								op.setUpdatetime(HelperUtil.getCurrentDate());
								orderProgressService.addOrderProgress(op);
							}else if (info.getType()==OrderCons.ORDER_TYPE_DAISONG) {
								JPush.pushWithAlias(JPush.getUserJPushClient(),OrderCons.FINISHED_SONGDA_STR, accountId);
								//记录消息
								int userid = orderService.getUserIdByOrderId(oid);
								Message message = new Message(OrderCons.FINISHED_SONGDA_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
								saveMessage(message);
								//保存进度
								OrderProgress op= new OrderProgress();
								op.setUserMsg(ProgressCons.ORDER_SENDED_MSG);
								op.setContent(ProgressCons.ORDER_SENDED_CONTENT);
								op.setOrderid(oid);
								op.setOrderStatus(OrderCons.FINISHED);
								op.setCreatetime(HelperUtil.getCurrentDate());
								op.setUpdatetime(HelperUtil.getCurrentDate());
								orderProgressService.addOrderProgress(op);
							}
						}
					}
				}).start();
				returnBean.setCode(ReturnBean.SUCCESS);
				returnBean.setMessage(ReturnBean.SUCCESS_STR);
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"OrderController", "finished", e);
			e.printStackTrace();
		}
		return returnBean;
	}

	/**
	 * 经纪人端已取货
	 * 
	 * @param popid
	 *            经纪人id
	 * @param orderid
	 *            订单id
	 * @return
	 */
	@RequestMapping(value = "/collect")
	@ResponseBody
	public ReturnBean collected(int popid, int orderid) {
		ReturnBean returnBean = new ReturnBean();
		try {
			int result = orderService.collected(popid, orderid);
			if (result > 0) {
				final int oid = orderid;
				final String accountId = orderService.getUserByOrderId(orderid);
				new Thread(new Runnable() {
					public void run() {
						// 发送给用户被抢单的通知
						JPush.pushWithAlias(JPush.getUserJPushClient(),
								OrderCons.COLLECTED_STR, accountId);
						//记录消息
						int userid = orderService.getUserIdByOrderId(oid);
						Message message = new Message(OrderCons.COLLECTED_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
						saveMessage(message);
						//保存进度
						OrderProgress op= new OrderProgress();
						op.setUserMsg(ProgressCons.ORDER_COLLECT_MSG);
						op.setContent(ProgressCons.ORDER_COLLECT_CONTENT);
						op.setOrderid(oid);
						op.setOrderStatus(OrderCons.COLLECTED);
						op.setCreatetime(HelperUtil.getCurrentDate());
						op.setUpdatetime(HelperUtil.getCurrentDate());
						orderProgressService.addOrderProgress(op);
					}
				}).start();
				returnBean.setCode(ReturnBean.SUCCESS);
				returnBean.setMessage(ReturnBean.SUCCESS_STR);
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"OrderController", "collected", e);
			e.printStackTrace();
		}
		return returnBean;
	}

	/**
	 * 获取经纪人任务列表
	 * 
	 * @param brokerid
	 * @return
	 */
	@RequestMapping("/tasklist")
	@ResponseBody
	public ReturnBean taskList(int brokerid, int startid, int size,String cityname) {
		ReturnBean returnBean = new ReturnBean();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("brokerid", brokerid);
			params.put("startid", startid);
			params.put("size", size);
			params.put("cityname", cityname);
			List<TaskBean> taskList = orderService.taskList(params);
			returnBean.setCode(ReturnBean.SUCCESS);
			returnBean.setMessage(ReturnBean.SUCCESS_STR);
			returnBean.setDatas(taskList);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"OrderController", "taskList", e);
			e.printStackTrace();
		}
		return returnBean;
	}

	/**
	 * 获取订单详情
	 * 
	 * @param orderid
	 * @return
	 */
	@RequestMapping("/info")
	@ResponseBody
	public OrderInfoBean getOrderInfo(int orderid) {
		OrderInfoBean orderInfo = orderService.getOrderInfo(orderid);
		if (orderInfo != null) {
			orderInfo.setCode(ReturnBean.SUCCESS);
			orderInfo.setMessage(ReturnBean.SUCCESS_STR);
		}
		return orderInfo;
	}
	
	/**
	 * 获取订单详情v1.1
	 * 
	 * @param orderid
	 * @return
	 */
	@RequestMapping("/v1_1/info")
	@ResponseBody
	public OrderInfoV1_1Bean getOrderInfoForV1_1(int orderid) {
		OrderInfoV1_1Bean orderInfo = orderService.getOrderInfoForV1_1(orderid);
		if (orderInfo != null) {
			orderInfo.setCode(ReturnBean.SUCCESS);
			orderInfo.setMessage(ReturnBean.SUCCESS_STR);
		}
		return orderInfo;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public OrderInfoV1_1_1Bean getOrderInfoForV1_1_1(int orderid) {
		OrderInfoV1_1_1Bean orderInfo = orderService.getOrderInfoForV1_1_1(orderid);
		if (orderInfo != null) {
			orderInfo.setCode(ReturnBean.SUCCESS);
			orderInfo.setMessage(ReturnBean.SUCCESS_STR);
		}
		return orderInfo;
	}
	
	@RequestMapping("v1_1_2/detail")
	@ResponseBody
	public OrderInfoV1_1_2Bean getOrderInfoForV1_1_2(int orderid) {
		OrderInfoV1_1_2Bean orderInfo = orderService.getOrderInfoForV1_1_2(orderid);
		if (orderInfo != null) {
			orderInfo.setCode(ReturnBean.SUCCESS);
			orderInfo.setMessage(ReturnBean.SUCCESS_STR);
		}
		return orderInfo;
	}
	
	/**
	 * 
	 * @param devicenumber
	 * @param orderid
	 * @param brokerid
	 * @return
	 * @author maxmin
	 * @date 2015年10月9日上午11:04:33
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ReturnBean deleteOrder(String devicenumber, int orderid, int brokerid) {
		ReturnBean rb = new ReturnBean();
		int result = orderService.deleteOrder(orderid, brokerid);
		if (result > 0) {
			String accountId = orderService.getUserByOrderId(orderid);
			// 发送给用户被抢单的通知
			JPush.pushWithAlias(JPush.getUserJPushClient(),
					OrderCons.CANCEL_STR, accountId);
			//记录消息
			int userid = orderService.getUserIdByOrderId(orderid);
			Message message = new Message(OrderCons.CANCEL_STR,userid,MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ORDER);
			saveMessage(message);
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		}
		return rb;
	}

	/**
	 * 获取经济人已完成记录
	 * @param brokerid
	 * @return
	 */
	@RequestMapping("/finished")
	@ResponseBody
	public ReturnBean completedRecord(int brokerid,int startid,int size,String devicenumber) {
		ReturnBean returnBean = new ReturnBean();
		try {
			List<TaskBean> list = orderService.completedRecord(brokerid,startid,size);
			returnBean.setCode(ReturnBean.SUCCESS);
			returnBean.setMessage(ReturnBean.SUCCESS_STR);
			returnBean.setDatas(list);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(), "OrderController", "completedRecord", e);
			e.printStackTrace();
		}
		return returnBean;
	}
}
